<template>
  <div class='container'>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="clearfix" slot="header" v-if="this.$route.query.id">
  <el-breadcrumb-item >学科管理</el-breadcrumb-item>
  <el-breadcrumb-item>{{this.$route.query.name}}</el-breadcrumb-item>
  <el-breadcrumb-item>活动列表</el-breadcrumb-item>
</el-breadcrumb>
    </div>
    <el-card class="box-card">
      <el-form :inline="true"  class="demo-form-inline">
    <el-form-item label="目录名称">
    <el-input v-model="input" type="text"></el-input>
  </el-form-item>
  <el-form-item label="状态">
    <el-select  placeholder="请选择" v-model="form.state">
      <el-option label="启用" value="1"></el-option>
      <el-option label="禁用" value="0"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button @click="clear">清除</el-button>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="list">搜索</el-button>
  </el-form-item>
  <el-form-item >
    <el-button class="el-icon-back" @click="comeBackFn" v-if="$route.query.id"> 返回学科</el-button>
  </el-form-item>
  <el-form-item >
    <el-button type="success" @click="AddObj">新增目录</el-button>
  </el-form-item>
</el-form>
    <el-row>
          <el-alert
            type="info"
            :closable="false"
            :title="`数据一共${total}条`"
            show-icon
            >
          </el-alert>
        </el-row>
      <el-table
      style="width: 100%"
      :data="SubjectList">
      <el-table-column
        label="序号"  type="index">
      </el-table-column>
      <el-table-column
        label="所属学科" prop="subjectName">
      </el-table-column>
      <el-table-column
        label="目录名称"  prop="directoryName">
      </el-table-column>
      <el-table-column
        label="创建者"  prop="username">
      </el-table-column>
      <el-table-column
        label="创建日期">
        <template slot-scope="{row}">
          {{row.addDate | parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column
        label="面试题数量" prop="totals">
      </el-table-column>
      <el-table-column
        label="状态" width="146px" >
         <template slot-scope="scope">
            {{ getState(scope.row.state) }}
         </template>
      </el-table-column>
      <el-table-column
        label="操作" >
        <template slot-scope="{row}">
          <el-button size="mini" type="text" @click="changeStateFN(row.id,row.state)">{{row.state===1?'禁用':'启用'}}</el-button>
          <el-button size="mini" type="text" :disabled="row.state===1" @click="AddObj(row.id,row.subjectName)">修改</el-button>
          <el-button size="mini" type="text" :disabled="row.state===1" @click="deleteFn(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end" align="middle">
          <el-pagination
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            :current-page="1"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="10"
            layout="total, prev, pager, next,sizes, jumper"
            :total="total">
          </el-pagination>
    </el-row>
    </el-card>
    <el-dialog
      :title="formDate.id?'修改目录':'新增目录'"
      :visible="show"
      width="30%"
      @close="btnCancel">
      <el-form ref="formDate" :model="formDate" label-width="80px" :rules="rules">
        <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
          <el-select placeholder="请选择" v-model="formDate.subjectID">
            <el-option :label="item.subjectName" :value="item.id" v-for="item,index in allList" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="formDate.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取消</el-button>
          <el-button @click="btnOk" type="primary">确认</el-button>
        </span>
      </el-dialog>
  </div>
</template>
<script>
import { list, remove, changeState, detail, add } from '@/api/hmmm/directorys'
import { lists } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      show: false,
      input: this.$route.query.name || '',
      page: 1,
      pagesize: 10,
      allList: [],
      total: 0,
      name: '',
      SubjectList: [], // 目录列表
      form: {
        directoryName: null, // 目录名称
        state: null // 状态`
      },
      formDate: {
        subjectID: null,
        directoryName: null // 目录名称
      },
      rules: {
        directoryName: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    console.log(this.$route.query.id, 'this.$route.query.id')
    this.list()
  },
  computed: {
    getState () {
      return (value) => {
        value = value * 1
        let val = null
        value === 1 ? val = '启用' : val = '禁用'
        return val
      }
    }
  },
  methods: {
    isShow () {
      this.show = true
    },
    btnCancel () {
      this.show = false
    },
    // 清除
    async clear () {
      this.input = ''
      await this.list()
    },
    // 弹出层确认
    async btnOk () {
      await this.$refs.formDate.validate()
      if (this.$route.query.id) {
        // console.log({ id: this.$route.query.id, subjectID: this.$route.query.id, ...this.formDate })
        this.formDate.subjectID = this.$route.query.id * 1
        await add(this.formDate)
      } else {
        await add(this.formDate)
      }
      this.list()
      this.$message.success('操作成功')
      this.show = false
    },
    // 修改状态
    async changeStateFN (id, state) {
      if (state === 1) {
        await changeState({ id: id, state: 0 })
      } else {
        await changeState({ id: id, state: 1 })
      }
      this.list()
      this.$message.success('切换成功')
    },
    async AddObj (id, name) {
      const datas = await lists({ page: this.page, pagesize: this.total })
      this.allList = datas.data.items
      const { data } = await detail({ id: id })
      this.formDate = { ...data }

      this.show = true
    },
    // 删除
    deleteFn (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取所有科目数据
    async list () {
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize,
        subjectName: this.input,
        directoryName: this.input,
        state: this.form.state
      })
      this.SubjectList = data.items
      this.total = data.counts
    },
    comeBackFn () {
      this.$router.back()
    },
    handleCurrentChange (newPage) {
      // newPage是当前点击的页码
      // console.log('handleCurrentChange');
      this.page = newPage
      this.list()
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.list()
    }
  }
}
</script>

<style scoped lang='scss'>
.el-button--primary{
    border: none;
}
::v-deep .el-input__inner {
    height: 32px;
    line-height: 32px;
}
.page-tools{
    padding: 0 16px;
    height: 35px;
    border-radius: 5px;
    background-color: #f4f4f5;
    margin-bottom: 15px;
    .write{
      line-height: 35px;
      font-size: 13px;
    }
  }
  .el-table{
    margin-bottom: 20px;
    // background-color: #fafafa;
  }
  ::v-deep .el-table th{
    background-color: #fafafa;
  }
  ::v-deep .el-pagination__jump{
    margin-left: 0;
  }
  ::v-deep .el-dialog__footer{
    text-align: right;
  }

</style>
